//
//  AddShopPLVC.m
//  ShopDemo
//
//  Created by Sunshine on 2018/8/7.
//  Copyright © 2018年 飞扬的青春. All rights reserved.
//

#import "AddShopPLVC.h"

@interface AddShopPLVC ()<UITextViewDelegate>

@property(nonatomic,strong)UITextView *contentextview;
@property(nonatomic,strong)NSString *wujiaoStr;

@end

@implementation AddShopPLVC

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = LineBackColor;
    self.navigationItem.title = @"新增评论";
    self.wujiaoStr = @"0";
    [self creatViewMthod];
    
}

-(void)creatViewMthod{
    self.contentextview = [UITextView new];
    self.contentextview.text = @"说点什么吧……";
    self.contentextview.backgroundColor= [UIColor whiteColor];
    self.contentextview.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    self.contentextview.font = [UIFont systemFontOfSize:15*kJLXWidthScale];
    self.contentextview.delegate = self;
    self.contentextview.layer.masksToBounds = YES;
    self.contentextview.layer.cornerRadius = 8*kJLXWidthScale;
    [self.view addSubview:self.contentextview];
    self.contentextview.sd_layout
    .topSpaceToView(self.view, 15*kJLXWidthScale)
    .leftSpaceToView(self.view, 15*kJLXWidthScale)
    .rightSpaceToView(self.view, 15*kJLXWidthScale)
    .heightIs(150*kJLXWidthScale);
    
    UILabel *lab = [UILabel new];
    lab.font = [UIFont systemFontOfSize:18*kJLXWidthScale];
    lab.text = @"评分：";
    [self.view addSubview:lab];
    lab.sd_layout
    .topSpaceToView(self.contentextview, 25*kJLXWidthScale)
    .leftSpaceToView(self.view, 15*kJLXWidthScale)
    .heightIs(35*kJLXWidthScale)
    .widthIs(70*kJLXWidthScale);
    
    for (int i = 0; i < 5; i ++) {
        MineButton *btn = [MineButton buttonWithType:UIButtonTypeCustom];
        btn.tag = 350 + i;
        [btn setImage:[UIImage imageNamed:@"grayW.png"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"yellowW.png"] forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(wuxingButtonAction:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:btn];
        btn.sd_layout
        .centerYEqualToView(lab)
        .leftSpaceToView(lab, 35*kJLXWidthScale*i + 10*i)
        .heightIs(35*kJLXWidthScale)
        .widthIs(35*kJLXWidthScale);
    }
    
    
    UIButton *savebtn = [UIButton buttonWithType:UIButtonTypeCustom];
    savebtn.layer.masksToBounds = YES;
    savebtn.layer.cornerRadius = 5*kJLXWidthScale;
    savebtn.layer.borderColor = [UIColor orangeColor].CGColor;
    savebtn.layer.borderWidth = 1;
    [savebtn setTitleColor:[UIColor darkGrayColor] forState:UIControlStateNormal];
    [savebtn setTitle:@"提交" forState:UIControlStateNormal];
    [savebtn addTarget:self action:@selector(saveButtonAction:) forControlEvents:UIControlEventTouchUpInside];
    savebtn.titleLabel.font = [UIFont systemFontOfSize:18*kJLXWidthScale];
    [self.view addSubview:savebtn];
    savebtn.sd_layout
    .topSpaceToView(lab, 50*kJLXWidthScale)
    .leftSpaceToView(self.view, 30*kJLXWidthScale)
    .rightSpaceToView(self.view, 30*kJLXWidthScale)
    .heightIs(45*kJLXWidthScale);
    
    
}

-(void)wuxingButtonAction:(MineButton *)sender{
    MineButton *btn0 = [self.view viewWithTag:350];
    MineButton *btn1 = [self.view viewWithTag:351];
    MineButton *btn2 = [self.view viewWithTag:352];
    MineButton *btn3 = [self.view viewWithTag:353];
    MineButton *btn4 = [self.view viewWithTag:354];
    
    if (sender.tag == 350) {
        sender.selected = !sender.selected;
        btn1.selected = NO;
        btn2.selected = NO;
        btn3.selected = NO;
        btn4.selected = NO;
        if (sender.selected == YES) {
            self.wujiaoStr = @"1";
        }else{
             self.wujiaoStr = @"0";
        }
        
    }else if (sender.tag == 351){
        sender.selected = !sender.selected;
        btn0.selected = YES;
        btn2.selected = NO;
        btn3.selected = NO;
        btn4.selected = NO;
        if (sender.selected == YES) {
            self.wujiaoStr = @"2";
        }else{
            self.wujiaoStr = @"1";
        }
    }else if (sender.tag == 352){
        sender.selected = !sender.selected;
        btn1.selected = YES;
        btn0.selected = YES;
        btn3.selected = NO;
        btn4.selected = NO;
        if (sender.selected == YES) {
            self.wujiaoStr = @"3";
        }else{
            self.wujiaoStr = @"2";
        }
    }else if (sender.tag == 353){
        sender.selected = !sender.selected;
        btn1.selected = YES;
        btn2.selected = YES;
        btn0.selected = YES;
        btn4.selected = NO;
        if (sender.selected == YES) {
            self.wujiaoStr = @"4";
        }else{
            self.wujiaoStr = @"3";
        }
    }else if (sender.tag == 354){
        sender.selected = !sender.selected;
        btn1.selected = YES;
        btn2.selected = YES;
        btn3.selected = YES;
        btn0.selected = YES;
        if (sender.selected == YES) {
            self.wujiaoStr = @"5";
        }else{
            self.wujiaoStr = @"4";
        }
    }
}

-(void)saveButtonAction:(MineButton *)sender{
    /*
     RATE_USERNAME 默认当前登录 必须
     RATE_USERID 当前登录用户ID 必须
     RATE_SCORE 评分0-5必须
     RATE_CONTENT 评论内容 必须
     RATE_GOODS 商品ID（必须）
     RATE_ATTRIBUTE 商品规格（必须）
     ORDER_ID 订单ID 必须

     
     */
    if (self.contentextview.text.length > 0 && ![self.contentextview.text isEqualToString:@"说点什么吧……"]) {
        NSArray *arr = self.maindic[@"orderdetail"];
        NSDictionary *goodic = arr[0];
        NSString *strurl = [NSString stringWithFormat:@"/dashop/order/app/rate.do?RATE_USERNAME=%@&RATE_USERID=%@&RATE_SCORE=%@&RATE_CONTENT=%@&RATE_GOODS=%@&RATE_ATTRIBUTE=%@&ORDER_ID=%@",[UserDefault objectForKey:LogUserName],[UserDefault objectForKey:LogUserID],self.wujiaoStr,self.contentextview.text,goodic[@"goods_id"],self.maindic[@""],self.maindic[@"order_id"]];
        NSString *endstr = [MethodClass returnUrlSetStrWSDLURL:strurl];
         NSString  *newUrlString = [endstr stringByAddingPercentEncodingWithAllowedCharacters:[NSCharacterSet URLQueryAllowedCharacterSet]];
        [self shopplMethodWithUrl:newUrlString];
    }else{
        KMLog(@"评论为空");
    }
    
    
}


-(void)shopplMethodWithUrl:(NSString *)url{
    [YQNetworking getWithUrl:url refreshRequest:YES cache:NO params:nil progressBlock:^(int64_t bytesRead, int64_t totalBytes) {
    } successBlock:^(id response) {
        NSDictionary *dic = response;
        [[NSOperationQueue mainQueue]addOperationWithBlock:^{
            if ([dic[@"RESULT"] integerValue] == 1) {
                [self.navigationController popViewControllerAnimated:YES];
                
            }
        }];
    } failBlock:^(NSError *error) {
        
    }];
}


#pragma mark - UITextViewDelegate
- (void)textViewDidEndEditing:(UITextView *)textView
{
    NSString *url;
    url = @"说点什么吧……";
    if(textView.text.length < 1){
        textView.text = url;
        textView.textColor = [[UIColor grayColor] colorWithAlphaComponent:0.5];
    }
}
- (void)textViewDidBeginEditing:(UITextView *)textView
{
    NSString *url;
    url = @"说点什么吧……";
    if([textView.text isEqualToString:url]){
        textView.text=@"";
        textView.textColor=[UIColor blackColor];
    }
}




@end
